<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" width="60%" destroy-on-close @closed="$emit('closed')" align-center>
		<el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="110px" label-position="right">
            <el-row :gutter="1">
                <el-col :span="8">
                    <el-form-item label="提报人" prop="salesman">
                        <el-select v-model="form.salesman" filterable clearable placeholder="请选择提报人" style="width:100%" disabled>
                            <el-option v-for="item in submitList" :key="item.id" :label="item.name" :value="item.id"/>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="报销金额合计" prop="money_all">
                        <el-input v-model="form.money_all" readonly></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="提报日期" prop="date">
                        <el-date-picker v-model="date" type="date" style="width: 100%;" :value-format="'YYYY-MM-DD'" disabled></el-date-picker>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <!-- <el-col :span="8">
                    <el-form-item label="审核状态" prop="dept_id">
                        <el-select v-model="form.profession" placeholder="请选择报销类型" clearable>
                            <el-option label="待审核" :value="1"/>
                            <el-option label="审核通过" :value="2"/>
                        </el-select>
                    </el-form-item>
                </el-col> -->
            </el-row>
            <el-row :gutter="1">
                <el-col :span="8">
                    <el-form-item label="酒店" prop="hotel_id">
                        <el-select v-model="form.hotel_id" placeholder="请选择所属酒店" style="width:100%" clearable filterable>
                            <el-option v-for="item in hotelList" :key="item.id" :label="item.hotel_name" :value="item.id"/>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="备注" prop="remark">
                        <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" clearable></el-input>
                    </el-form-item>
                </el-col>
                <!-- <el-col :span="12">
                    <el-form-item label="附件" prop="referrer_id">
                        <sc-upload-file v-model="form.attachment">
                            <el-button type="primary" size="small">上传文件</el-button>
                        </sc-upload-file>
                    </el-form-item>
                </el-col> -->
            </el-row>
            <el-form-item label="报销清单" prop="operate_detail">
                <sc-form-table ref="table" class="formtable" v-model="form.operate_detail" :addTemplate="operate" placeholder="暂无数据" height="350" show-summary>
                    <el-table-column label="报销类型" align="center" width="200">
                        <template #default="scope">
                            <el-select v-model="scope.row.operate_type" placeholder="请选择报销类型" clearable filterable>
                                <el-option label="季节性报销" :value="1"/>
                                <el-option label="办公用品报销" :value="2"/>
                                <el-option label="公车加油报销" :value="3"/>
                                <el-option label="公关应酬报销" :value="4"/>
                                <el-option label="物料损耗报销" :value="5"/>
                                <el-option label="仓库运营报销" :value="6"/>
                            </el-select>
                        </template>
                    </el-table-column>
                    <el-table-column label="报销明细" align="center">
                        <template #default="scope">
                            <el-input v-model="scope.row.detail" placeholder="明细"></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column label="金额" align="center" width="150" prop="money">
                        <template #default="scope">
                            <el-input v-model="scope.row.money" placeholder="金额" @focus="onFocus($event)" @input="count"></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column label="附件" align="center">
                        <template #default="scope">
                            <sc-upload-multiple width='50' height='50' v-model="scope.row.operate_files" draggable></sc-upload-multiple>
                        </template>
                    </el-table-column>
                    <el-table-column label="备注" align="center">
                        <template #default="scope">
                            <el-input v-model="scope.row.remark" placeholder="备注"></el-input>
                        </template>
                    </el-table-column>
                </sc-form-table>
            </el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
export default {
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增运营报销',
				edit: '编辑运营报销',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
                id:"",
				operate_code: "",
				money_all: "",
				remark: "",
				salesman: this.$TOOL.data.get("USER_INFO").admin_id,
				hotel_id: "",
				operate_detail: [],
			},
            date:this.moment().format('YYYY-MM-DD'),
			//验证规则
			rules: {
				enterprise: [{ required: true, message: '请输入名称' }],
			},
            operate:{},
            submitList:[],
            hotelList:[],
		}
	},
	mounted() {
        this.getUser()
        this.getHotel()
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = [];
					if (this.form.id) {
						res = await this.$API.dispatch.operate.edit.post(this.form);
					} else {
						res = await this.$API.dispatch.operate.add.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", { type: 'error' })
					}
				} else {
					return false;
				}
			})
		},
		//表单注入数据
		async setData(data) {
            this.form.id = data.id
            var res = await this.$API.dispatch.operate.detail.get({id:data.id});
            this.form.operate_code = res.data.operate_code
            this.form.money_all = res.data.money_all
            this.form.hotel_id = res.data.hotel_id
            this.form.remark = res.data.remark
            this.form.salesman = Number(res.data.salesman)
            let array = []
            res.data.operate_detail.map(item=>{
                array.push({
                    id:item.id,
                    operate_type:item.operate_type,
                    detail:item.detail,
                    money:item.money,
                    operate_files:item.operate_files,
                    remark:item.remark
                })
            })
            this.form.operate_detail = array
            this.date = data.create_time
		},
        onFocus(event){
            event.target.select();
        },
        async getUser(){
            var res = await this.$API.system.admin.all.get();
            this.submitList = res.data;
        },
        async getHotel(){
            var res = await this.$API.client.hotel.list.get();
            this.hotelList = res.data.rows;
        },
        count(){
            let count_price = 0
            this.form.operate_detail.map(item => {
                if(item.money){
                    count_price = Number(count_price) + Number(item.money)
                }
            })
            this.form.money_all = (count_price).toFixed(2)
        },
	}
}
</script>

<style></style>
